//
//  WZNewspaperArticleImageViewController.m
//  复合发行3.0
//
//  Created by liubowen on 16/6/2.
//  Copyright © 2016年 Beijing WZYK Technology co.,ltd. All rights reserved.
//

#import "WZNewspaperArticleImageViewController.h"
#import "UIImageView+WebCache.h"
//#import "TMTeacherDetailController.h"


@interface WZNewspaperArticleImageViewController ()<UICollectionViewDelegate,
UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *imageCollectionView;
@property (weak, nonatomic) IBOutlet UILabel *indexLabel;
//@property (nonatomic, strong) TMTeacherDetailController *detailVC;
@end


@interface WZMagazineArticleImageCell ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UIView *scrollContentView;
@property (weak, nonatomic) IBOutlet UIScrollView *scaleScrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthOfScrollContentViewConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightOfScrollContentViewConstraint;
@end

@implementation WZNewspaperArticleImageViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 1. 创建UICollectionViewFlowLayout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat itemWidth = self.view.bounds.size.width;
    CGFloat itemHigth = self.imageCollectionView.bounds.size.height * 0.9;
    // 1.1 设置item大小
    flowLayout.itemSize = CGSizeMake(itemWidth, itemHigth);
    // 1.2 设置每列间距
    flowLayout.minimumInteritemSpacing = 20;
    // 1.3 设置每行间距
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.imageCollectionView.collectionViewLayout = flowLayout;
    
    
}
- (void)show{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.indexLabel.text = [NSString stringWithFormat:@"%@/%@",@(self.currentImageIndex + 1), @(self.imageList.count)];
    [self.imageCollectionView reloadData];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.currentImageIndex inSection:0];
    [self.imageCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark - Delegate of <UICollectionViewDelegate> method


#pragma mark -
#pragma mark - DataSource of <UICollectionViewDataSource> method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.imageList.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WZMagazineArticleImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WZMagazineArticleImageCellID" forIndexPath:indexPath];
    if (self.imageList && self.imageList.count != 0) {
        [cell.image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithBaseUrl:self.imageList[indexPath.row]]] ];
    }
    cell.block = ^{
        [self.navigationController popViewControllerAnimated:YES];
    };
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger count = (scrollView.contentOffset.x + (self.imageCollectionView.bounds.size.width / 2)) / self.imageCollectionView.bounds.size.width;
    self.indexLabel.text = [NSString stringWithFormat:@"%@/%@",@(count+1),@(self.imageList.count)];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


@implementation WZMagazineArticleImageCell

- (void)awakeFromNib{
    [super awakeFromNib];
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap)];
    doubleTap.numberOfTapsRequired = 2;
    [self addGestureRecognizer:doubleTap];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [tap requireGestureRecognizerToFail:doubleTap];
    
    self.image.userInteractionEnabled = YES;
    [self.image addGestureRecognizer:tap];
    
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    NSLog(@"Size:%@",NSStringFromCGRect(self.frame));
    self.widthOfScrollContentViewConstraint.constant = self.bounds.size.width;
    self.heightOfScrollContentViewConstraint.constant = self.bounds.size.height;
    
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return self.scrollContentView;
}

//单击隐藏
- (void)tap{
    NSLog(@"Tap");
    //[self.articleViewController hideImageContainerView];
    if (self.block) {
        self.block();
    }
}
// 双击放大
- (void)doubleTap
{
    NSLog(@"doubleTap>>>>%f",self.scaleScrollView.zoomScale);
    [UIView animateWithDuration:0.3 animations:^{
        if (self.scaleScrollView.zoomScale <= 1) {
            self.scaleScrollView.zoomScale = 2;
        }
        else {
            self.scaleScrollView.zoomScale = 1;
        }
    }];
}


@end

